package com.epam.edu.command;

import com.epam.edu.dao.DAOInputMark;
import com.epam.edu.dao.DAOSpecialty;
import com.epam.edu.dao.DAOSubject;
import com.epam.edu.db.ConnectionPool;
import com.epam.edu.entity.InputMark;
import com.epam.edu.entity.Specialty;
import com.epam.edu.entity.Subject;
import com.epam.edu.util.Parser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class SaveInputMarksCommand implements Command {
    @Override
    public String perform(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, NoSuchAlgorithmException {
        ConnectionPool pool = null;
        Connection connection = null;
        try {
            ConnectionPool.init();
            pool = ConnectionPool.getInstance();
            connection = pool.takeConnection();
            addNewInputMark(connection, request);
            updateInputMark(connection, request);
            deleteCheckInputMark(connection, request);
            List<InputMark> inputMarks = DAOInputMark.selectAll(connection);
            request.setAttribute("inputMarks", inputMarks);
            List<Specialty> specialties = DAOSpecialty.selectAll(connection);
            request.setAttribute("specialties", specialties);
            List<Subject> subjects = DAOSubject.selectAll(connection);
            request.setAttribute("subjects", subjects);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "/WEB-INF/jsp/admin/catalogInputMarks.jsp";
    }

    private void updateInputMark(Connection connection, HttpServletRequest request) {
        String[] inputMarkId = request.getParameterValues("idInputMark");
        String[] specialties = request.getParameterValues("changeSpecialty");
        String[] historyKZMarks = request.getParameterValues("changeHistoryKZMark");
        String[] languageMarks = request.getParameterValues("changeLanguageMark");
        String[] mathematicsMarks = request.getParameterValues("changeMathematicsMark");
        String[] subjects = request.getParameterValues("changeSbject");
        String[] marks = request.getParameterValues("changeMark");
        for (int i = 0; i < inputMarkId.length; i++) {
            InputMark inputMark = DAOInputMark.selectById(connection, Integer.parseInt(inputMarkId[i]));
            inputMark.setSpecialty(DAOSpecialty.selectByCode(connection, specialties[i]));
            inputMark.setHistoryKZMark(Integer.parseInt(historyKZMarks[i]));
            inputMark.setLanguageMark(Integer.parseInt(languageMarks[i]));
            inputMark.setMathematicsMark(Integer.parseInt(mathematicsMarks[i]));
            inputMark.setSubject(DAOSubject.selectByName(connection, subjects[i]));
            inputMark.setMark(Integer.parseInt(marks[i]));
            DAOInputMark.update(connection, inputMark);
        }
    }

    private void addNewInputMark(Connection connection, HttpServletRequest request) {
        String newSpecialty = request.getParameter("newSpecialty");
        if ("-".equals(newSpecialty)) return;
        Specialty specialty = DAOSpecialty.selectByCode(connection, Parser.dividedIntoTwoParts(newSpecialty,'-')[0]);
        int newHistoryKZMark = Parser.stringToIntParser(request.getParameter("newHistoryKZMark"));
        int newLanguageMark = Parser.stringToIntParser(request.getParameter("newLanguageMark"));
        int newMathematicsMark = Parser.stringToIntParser(request.getParameter("newMathematicsMark"));
        int newMark = Parser.stringToIntParser(request.getParameter("newMark"));
        Subject newSubject = DAOSubject.selectByName(connection, request.getParameter("newSubject"));
        if ("-".equals(newSubject)) return;
        int id = DAOInputMark.add(connection, new InputMark(specialty, newHistoryKZMark, newLanguageMark, newMathematicsMark, newSubject, newMark));
    }

    private void deleteCheckInputMark(Connection connection, HttpServletRequest request) {
        String[] checkboxes = request.getParameterValues("checkbox");
        if (checkboxes != null) {
            for (String s : checkboxes) {
                int id = Integer.parseInt(s);
                InputMark inputMark = DAOInputMark.selectById(connection, id);
                if (inputMark != null) {
                    DAOInputMark.delete(connection, inputMark);
                }
            }
        }
    }
}
